using Google.Protobuf;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;

namespace SinceTimes.NetWork
{

 

    public interface IDataPackage
    {
        ushort SizeHeader { get; }

        public byte[] Buff { get; }

        ushort Length { get; }
        ushort Type { get; }

        public bool Avilable { get; }

        public IDataPackage UnPack(int len);

    }

    public class DataPackager: IDataPackage
    {
        IDataPackage IDataPackage.UnPack(int len) => UnPack(len);
        public bool Avilable { get; set; }

        public ushort Length { get; set; }
        public ushort Type { get; set; }

        public ushort SizeHeader => SIZE_HEAD;

        //接收缓存得大小(字节)
        const uint BUFF_SIZE = 4096;
        // 封包:长度
        const ushort SIZE_LENGTH = 2;
        //封包:消息类型
        const ushort SIZE_TYPE = 2;
        //封包:消息序列号
        const ushort SIZE_SERIAL = 0;
        //封包:消息头 总长度 =10个字节.
        static ushort SIZE_HEAD => SIZE_LENGTH + SIZE_TYPE + SIZE_SERIAL;

        public byte[] Buff => _Buff_Recive;

      

        byte[] _Buff_Recive = new byte[BUFF_SIZE];


        public DataPackager()
        {

        }
        public DataPackager UnPack(int len)
        {
            Avilable = false;
            if (len < SIZE_HEAD)
                return this;
            Length = Net.BYTE_USHORT(_Buff_Recive, 0);
            Type = Net.BYTE_USHORT(_Buff_Recive, SIZE_LENGTH);


            if (Net.SHOWLOG) Debug.Log($"[DataPackager.UnPack]:Len={Length} ; type={Type}");

            Avilable = true;
            return this;
        }



    }
    public class Message : ProtobufMsg
    {
        public Message(ushort id, IMessage _msg) : base((ushort)id, _msg)
        {

        }
    }
}

